package com.weavernorth.store.service;

import com.weaver.general.Util;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import oracle.jdbc.internal.OracleTypes;
import org.apache.log4j.Logger;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.StaticObj;
import weaver.interfaces.datasource.DataSource;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.weavernorth.util.dateUtils.getYearMonthDay;
import static com.weavernorth.util.dateUtils.isValidDate;

/**
 * @Author: BruseeWang
 * @Date: 2020/11/17 14:47
 * @Description: 从V8获取门店打卡记录插入到建模表
 */
public class getDataFromV8Service {
    private Logger log = Logger4jUtil.getLoggerByName("/Timmer/getDataFromV8Timmer/");
    private String gh;
    private String begindate;
    private String endate;
    public String getGh() {
        return gh;
    }
    public void setGh(String gh) {
        this.gh = gh;
    }
    public String getBegindate() {
        return begindate;
    }
    public void setBegindate(String begindate) {
        this.begindate = begindate;
    }
    public String getEndate() {
        return endate;
    }
    public void setEndate(String endate) {
        this.endate = endate;
    }

    public void execute(){
        log.info("V8获取巡店人员打卡记录：");
        log.info("Begin>>>>>>>>>>>>");
        String strBeginDate="";//开始日期
        String strEndate="";//结束日期
        String strGh=(gh==null)?"":gh;
        //获取当前日期的前一天
        String strYesterday= DateUtil.getYesterday();
        strBeginDate=(begindate==null) ? (strYesterday+" 00:00:00"):(isValidDate(begindate,"yyyy-MM-dd HH:mm:ss") ? begindate : (strYesterday+" 00:00:00"));
        strEndate=(endate==null)? (strYesterday+" 23:59:59"):(isValidDate(endate,"") ? endate : (strYesterday+" 23:59:59"));
        try{
            getDataFromV8(strGh,strBeginDate, strEndate,log);
        }catch (Exception e){
            log.info("接口异常,异常信息："+e.toString());
        }
        log.info("end>>>>>>>>>>>>");
    }
    /***
     * 从V8获取门店打卡数据
     * @param strWorkCode 员工号
     * @param beginDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    public static void getDataFromV8(String strWorkCode, String beginDate, String endDate,Logger log) throws SQLException {
        CallableStatement callStmt = null;
        DataSource ds = null;
        Connection conn = null;
        try {
            Date begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beginDate);
            Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
            if(begin.compareTo(end)==1){//如果开始日期大于结束日期,则重新赋值结束日期;
                endDate=beginDate.substring(0,10)+" 23:59:59";
            }
            ds = (DataSource) StaticObj.getServiceByFullname(
                    ("datasource.V8A"), DataSource.class);
            conn = ds.getConnection();
            // 存储过程 P_ERDOS_YHKQ_QRY 查询数据
            callStmt = conn.prepareCall("{call fyerp.P_ERDOS_YHKQ_QRY(?,?,?,?,?,?)}");
            callStmt.setString(1, strWorkCode);//工号
            callStmt.setString(2, beginDate);//起始日期
            callStmt.setString(3, endDate);//截止日期
            callStmt.registerOutParameter(4, Types.FLOAT);
            callStmt.registerOutParameter(5, Types.VARCHAR);
            callStmt.registerOutParameter(6, OracleTypes.CURSOR);
            callStmt.execute();
            String delete_sql="delete uf_v8record where dkdate>='"+beginDate+"' and dkdate<='"+endDate+"'";
            log.info("先删除日期段内数据sql:"+delete_sql);
            RecordSet rs_delete=new RecordSet();
            rs_delete.execute(delete_sql);//执行删除操作
            //获取信息结果集
            ResultSet rsResult = (ResultSet) callStmt.getObject(6);
            log.info("获取门店打卡数据,工号：" + strWorkCode + ";开始时间：" + beginDate + ";结束时间：" + endDate);
            //循环结果集
            while (rsResult.next()) {
                //微信id
                String strVxId = Util.null2String(rsResult.getString(1));
                //工号
                String strGh = Util.null2String(rsResult.getString(2));
                //手机号
                String strPhone = Util.null2String(rsResult.getString(3));
                //打卡门店
                String strDkmd = Util.null2String(rsResult.getString(4));
                //打卡时间
                String strDksj = Util.null2String(rsResult.getString(5));
                strDksj=strDksj.substring(0,19);
                //打卡IP
                String strDkIp = Util.null2String(rsResult.getString(6));
                //打卡机器MAC地址
                String strMac = Util.null2String(rsResult.getString(7));
                //门店地址
                String strMdAddress = Util.null2String(rsResult.getString(8));
                //拆分打卡时间的日期和时间分别保存
                String strDkdate=strDksj.substring(0,10);
                String strDktime=strDksj.substring(11,19);
                if(strGh.length()<9){
                    insertData(strGh,strDkmd,strDkdate,strDktime,strDksj,strDkIp,strMac,strMdAddress,strVxId,strPhone,log);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("执行存储过程发生异常>>>>>>>>" + e.toString());
        } finally {
            if (null != callStmt) {
                callStmt.close();
            }
            if (null != conn) {
                conn.close();
            }
        }
    }
    /***
     * 打卡数据插入到打卡建模表uf_v8record
     * @param strDkDate
     * @param strDkTime
     * @param strDkIp
     * @param strMac
     * @param strMdAddress
     */
    private static void insertData(String strGh, String strDkmd,String strDkDate, String strDkTime,  String strDksj,String strDkIp, String strMac, String strMdAddress,String strVxId,String strPhone,Logger log) {
        RecordSetTrans addTrans = new RecordSetTrans();
        String strInsert="INSERT INTO uf_v8record (gh,dkmd,dkdate,dkTime,dkip,dksj,macaddress,mdAddress,vxid,phone)VALUES"+
                "('"+strGh+"','"+strDkmd+"','"+strDkDate+"','"+strDkTime+"','"+strDkIp+"','"+strDksj+"','"+strMac+"','"+strMdAddress+"','"+strVxId+"','"+strPhone+"')";
        log.info("插入数据sql"+strInsert);
        try{
            addTrans.setAutoCommit(false);
            addTrans.executeSql(strInsert);
            addTrans.commit();
            log.info("打卡数据插入成功！");
        }catch (Exception ex){
            ex.printStackTrace();
            log.info("插入打卡记录异常,异常信息："+ex.toString());
            log.info("插入打卡记录sql:"+strInsert);
            addTrans.rollback();
        }
    }


}
